<template>
  <div class="article-detail__container">
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.go(-1)" />

    <!-- 文章信息区域 -->
    <div class="article-container"    v-if="!loading"   >
      <!-- 文章标题 -->
      <h1 class="art-title">{{aetOoj.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="aetOoj.aut_name" :label=" $timeAgo(aetOoj.pubdate)   ">
        <template #icon>
          <img :src="aetOoj.aut_photo" alt="avatar" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button   v-if="aetOoj.is_followed"
              type="info" size="mini"
               @click="followOrUnfollowFn"

              >已关注</van-button>

            <van-button
             v-else icon="plus" type="info"
              size="mini" plain
                @click="followOrUnfollowFn"
              >关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content"   v-html="aetOoj.content">好好学习, 天天向上</div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">

        <van-button   v-if="aetOoj.attitude===1"
            icon="good-job" type="danger" size="small"
             @click="likeOrDislikeFn"
            >已点赞</van-button>

        <van-button   v-else       icon="good-job-o" type="danger"
        plain size="small"
         @click="likeOrDislikeFn"
        >点赞</van-button>
      </div>
    </div>
    <!-- 评论 -->
    <comment-list/>
  </div>
</template>

<script>
import {
  getArticleDetailAPI,
  followAutAPI,
  unfollowAutAPI,
  likeArticeAPI,
  dislikeArticeAPI
} from '@/api/article'

import CommentList from './commentlist.vue'
export default {
  name: 'ar1',
  components: { CommentList },
  data () {
    return {
      aetOoj: {}, // 详情对象
      loading: false // 加载提示
    }
  },
  // 详情
  async  created () {
    this.loading = true
    const t = this.$toast.loading({

      message: '加载中',
      forbikClick: true,
      duration: 0

    })

    const res = await getArticleDetailAPI({
      articleld: this.$route.query.art_id
    })
    this.loading = false
    t.clear()
    this.aetOoj = res.data
  },
  // 关注 取关
  methods: {
    async  followOrUnfollowFn () {
      try {
        this.aetOoj.is_followed ? await unfollowAutAPI({
          authorld: this.aetOoj.aut_id
        })
          : await followAutAPI({
            authorld: this.aetOoj.aut_id
          })
        this.aetOoj.is_followed = !this.aetOoj.is_followed

        this.$toast.success(this.aetOoj.is_followed ? '取关成功' : '关注成功')
      } catch (e) {
        this.$toast.fail(this.aetOoj.is_followed ? '取关败' : '关注败')
      }
    },

    async   likeOrDislikeFn () {
      try {
        this.aetOoj.attitude === 1 ? await dislikeArticeAPI({ articleld: this.aetOoj.art_id }) : await likeArticeAPI({ articleld: this.aetOoj.art_id })

        this.$toast.success(this.aetOoj.is_followed ? '赞关成功' : '赞注成功')
        this.aetOoj.attitude = this.aetOoj.attitude === 1 ? -1 : 1
      } catch (e) {
        this.$toast.fail(this.aetOoj.is_followed ? '赞关败' : '赞注败')
      }
    }
  }

}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{

        width: 100%;
}
    /deep/ pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
